package cn.sumpu.app.guoguojie.biz.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;

import cn.sumpu.app.guoguojie.biz.DoingBiz;
import cn.sumpu.app.guoguojie.biz.DoingLoveBiz;
import cn.sumpu.app.guoguojie.biz.IdentifierBiz;
import cn.sumpu.app.guoguojie.dao.DoingDao;
import cn.sumpu.app.guoguojie.dao.DoingLoveDao;
import cn.sumpu.app.guoguojie.domain.Doing;
import cn.sumpu.app.guoguojie.domain.DoingLove;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
import cn.sumpu.app.guoguojie.util.Common;
import cn.sumpu.app.guoguojie.util.PageModel;

public class DoingLoveBizImpl implements DoingLoveBiz {
	
	@Resource
	private IdentifierBiz identifierBiz;
	@Resource
    private DoingBiz doingBiz;
	
	private DoingLoveDao doingLoveDao;

	private DoingDao doingDao;

	public void setDoingLoveDao(DoingLoveDao doingLoveDao) {
		this.doingLoveDao = doingLoveDao;
	}

	public void setDoingDao(DoingDao doingDao) {
		this.doingDao = doingDao;
	}

	@Override
	public void insertDoingLove(DoingLove doingLove)
			throws AppBusinessException {
		doingLoveDao.insert(doingLove);

	}

	@Override
	public void updateDoingLove(DoingLove doingLove)
			throws AppBusinessException {
		doingLoveDao.update(doingLove);

	}

	public void deleteDoingLoveById(String id) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("identifier", id);
		doingLoveDao.deleteLoveDoing(map);

	}

	@Override
	public DoingLove getDoingLoveById(String id) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("identifier", id);
		return doingLoveDao.getDoingLove(map);
	}

	@Override
	public Integer getMyLoveCount(String family) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("receiveOwner", family);
		return doingLoveDao.getListCount(map);
	}

	// 我的喜欢
	public PageModel getMyDoingLoveList(String family, Integer page)
			throws AppBusinessException {
		int pageSize = Common.PAGE_SIZE_DOING_LOVE;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("createOwner", family);
		map.put("lovePicture", "lovePicture");
		int count = doingLoveDao.myLoveDoingListCount(map);
		map.put("pageSize", pageSize);
		map.put("startId", (page - 1) * pageSize);
		PageModel pm = new PageModel();
		List<Doing> list = doingLoveDao.myLoveDoingList(map);
		for (Doing tmp : list) {
			String rootId = tmp.getRootId();
			Doing rootDoing = doingDao.getDoingById(rootId);
			tmp.setRootDoing(rootDoing);
		}

		pm.setTotalRecord(count);
		pm.setCurrentPage(page);
		pm.setPageSize(pageSize);
		pm.setTotalPage((count + pageSize - 1) / pageSize);
		pm.setList(list);
		return pm;
	}

	// 喜欢我的
	public PageModel loveMeList(String family, Integer page)
			throws AppBusinessException {
		if (Common.empty(page)) {
			page = 1;
		}
		int pageSize = Common.PAGE_SIZE_POSTDOING_MYFAMILY;
		PageModel pm = new PageModel();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("receiveOwner", family);
		int totalRecord = doingLoveDao.loveMeListCount(map);
		map.put("startId", (page - 1) * pageSize);
		map.put("pageSize", pageSize);
		List<Doing> doingList = doingLoveDao.loveMeList(map);
		for (Doing tmp : doingList) {
			// tmp.setContent(Common.atContentRep(tmp.getReplaceContent()));
			String rootId = tmp.getRootId();
			if (!Common.empty(rootId)) {
				Doing rootDoing = doingDao.getDoingById(rootId);
				tmp.setRootDoing(rootDoing);
			}

		}
		pm.setList(doingList);

		pm.setTotalRecord(totalRecord);
		pm.setCurrentPage(page);
		pm.setPageSize(pageSize);
		pm.setTotalPage((totalRecord + pageSize - 1) / pageSize);
		return pm;
	}

	// 喜欢
	@Transactional(rollbackForClassName = { "cn.sumpu.app.guoguojie.exception.AppBusinessException" }, timeout = 10)
	public String loveMe(DoingLove doingLove)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("doing", doingLove.getDoing());
		map.put("createMember", doingLove.getCreateMember());
		List<DoingLove> list = doingLoveDao.getList(map);
		if (list != null && list.size() > 0) {
			return "alreadyLove";
		} else {
			doingLove.setIdentifier(identifierBiz.generateId("doing_love"));
			doingLoveDao.insert(doingLove);
			Doing doing = new Doing();
			doing.setIdentifier(doingLove.getDoing());
			doingBiz.updateDoingAdd(doingLove.getDoing(), 1);
			return "loveSuccess";
		}
	}

	// 删除我家喜欢的一条微博
	@Transactional(rollbackForClassName = { "cn.sumpu.app.guoguojie.exception.AppBusinessException" }, timeout = 10)
	public void deleteDoingLoveByFamily(String doingId, String family)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("doing", doingId);
		map.put("createOwner", family);
		doingLoveDao.deleteLoveDoing(map);
		Doing doing = doingDao.getDoingById(doingId);
		int loveAmount = doing.getLoveAmount() - 1;
		doing.setLoveAmount(loveAmount);
		doingDao.update(doing);

	}

	@Override
	public int getMyFamilyLoveAmount(String family) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("owner", family);
		return doingLoveDao.getMyFamilyLoveAmount(map);
	}

	@Override
	public int getMyLoveDoingCount(String family)throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("family", family);
		return doingLoveDao.getMyLoveDoingCount(map);
	}

    @Override
    public int getMemberCreateLoveCount(String member) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("createMember", member);
        return doingLoveDao.getMemberLoveCount(map);
    }

    @Override
    public int getMemberReceiveLoveCount(String member) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("receiveMember", member);
        return doingLoveDao.getMemberLoveCount(map);
    }

}
